package com.peng.dao.impl;

import com.peng.dao.userDao;
import com.peng.dao.workDao;
import com.peng.entity.work;
import com.peng.util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 欧阳欣鹏 on 2018/8/7.
 */
public class workDaoImpl implements workDao
{
    @Override
    public List<work> li(int status,int workType) {
        List<work> li=new ArrayList<work>();
        String sql="select * from work where 1=1";
        if (status!=0&&workType!=0){
            sql+=" and status=? and workType=?";
        }

        Connection con=null;
        PreparedStatement st=null;
        ResultSet rs=null;
        try {
            con= BaseDao.getConnection();
            st=con.prepareStatement(sql);
            st.setInt(1,status);
            st.setInt(2,workType);
            rs=st.executeQuery();
            while (rs.next()) {
                work w=new work();
                w.setWorkid(rs.getInt("workid"));
                w.setStaffid(rs.getInt("staffid"));
                w.setUserName(rs.getString("userName"));
                w.setUserPhone(rs.getString("userPhone"));
                w.setUserAddress(rs.getString("userAddress"));
                w.setWorkType(rs.getInt("workType"));
                w.setStatus(rs.getInt("status"));
                w.setWpid(rs.getInt("wpid"));
                userDao daoo=new userDaoImpl();
                w.setS(new staffDaoImpl().li(w.getStaffid()));
               w.setSslist(daoo.li(w.getS().getUserid()));
                li.add(w);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            BaseDao.closeConnection(con, st, rs);
        }
        return li;
    }

    @Override
    public int delete(int id) {
        String sql="DELETE FROM `work` where workid=? ";
        Object[] ob={id};
        return BaseDao.idu(sql,ob);
    }

    @Override
    public int insert(work w) {
        String sql="insert into work value(null,?,?,?,?,?,?,?)";
        Object[] ob={w.getStaffid(),w.getUserName(),w.getUserPhone(),w.getUserAddress(),w.getWorkType(),w.getStatus(),w.getWpid()};
        return BaseDao.idu(sql,ob);
    }

    @Override
    public int findbyIdStaff() {
        int count=0;
        String sql="select max(staffid) from staff";
        Connection con=null;
        PreparedStatement st=null;
        ResultSet rs=null;
        try {
            con= BaseDao.getConnection();
            st=con.prepareStatement(sql);
            rs=st.executeQuery();
            if (rs.next()) {
                count=rs.getInt(1);

            }
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            BaseDao.closeConnection(con, st, rs);
        }
        return count;
    }

    @Override
    public int findbyIdProduct() {
        int count=0;
        String sql="select max(wpid) from product";
        Connection con=null;
        PreparedStatement st=null;
        ResultSet rs=null;
        try {
            con= BaseDao.getConnection();
            st=con.prepareStatement(sql);
            rs=st.executeQuery();
            if (rs.next()) {
                count=rs.getInt(1);

            }
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            BaseDao.closeConnection(con, st, rs);
        }
        return count;
    }

    @Override
    public work findbuId(int id) {
        work w=new work();
        String sql="select * from work where workid=?";
        Connection con=null;
        PreparedStatement st=null;
        ResultSet rs=null;
        try {
            con= BaseDao.getConnection();
            st=con.prepareStatement(sql);
            st.setInt(1,id);
            rs=st.executeQuery();
            if (rs.next()) {
                w.setWorkid(rs.getInt("workid"));
                w.setStaffid(rs.getInt("staffid"));
                w.setUserName(rs.getString("userName"));
                w.setUserPhone(rs.getString("userPhone"));
                w.setUserAddress(rs.getString("userAddress"));
                w.setWorkType(rs.getInt("workType"));
                w.setStatus(rs.getInt("status"));
                w.setWpid(rs.getInt("wpid"));

            }
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            BaseDao.closeConnection(con, st, rs);
        }
        return w;

    }

    @Override
    public int update(work w) {
        String sql="update work set userName=?,userPhone=?,userAddress=? where workid=?";
        Object[] ob={w.getUserName(),w.getUserPhone(),w.getUserAddress(),w.getWorkid()};
        return BaseDao.idu(sql,ob);
    }
}
